package com.boc.ewr.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.boc.ewr.business.entity.AcctClassCFdbk;
import com.boc.ewr.business.entity.AcctDtlClassA1;
import com.boc.ewr.business.entity.AcctDtlClassC;
import com.boc.ewr.business.mapper.AcctClassCFdbkMapper;
import com.boc.ewr.business.mapper.AcctDtlClassCMapper;
import com.boc.ewr.business.service.IAcctClassCFdbkService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 《风险核查反馈清单》-C类账户
 * @Author: jeecg-boot
 * @Date:   2023-05-26
 * @Version: V1.0
 */
@Service
public class AcctClassCFdbkServiceImpl extends ServiceImpl<AcctClassCFdbkMapper, AcctClassCFdbk> implements IAcctClassCFdbkService {

    @Autowired
    private AcctDtlClassCMapper acctDtlClassCMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createData(String caMon, String caPeriodNo, Date startTime, Date endTime) {
        // 先删除所有之前数据
        this.remove(null);

        // 生成wrapper
        LambdaQueryWrapper<AcctDtlClassC> cWrapper = new LambdaQueryWrapper();
        if (!"".equals(caMon)) {
            cWrapper.eq(AcctDtlClassC::getCaMon,caMon);
        }
        if (!"".equals(caPeriodNo)) {
            cWrapper.eq(AcctDtlClassC::getCaPeriodNo,caPeriodNo);
        }
        if (startTime!=null&&endTime!=null){
            cWrapper.between(AcctDtlClassC::getCreateTime,startTime,endTime);
        }else if(startTime==null&&endTime!=null){
            cWrapper.le(AcctDtlClassC::getCreateTime,endTime);
        }else if(startTime!=null&&endTime==null){
            cWrapper.ge(AcctDtlClassC::getCreateTime,startTime);
        }

        // C转化
        List<AcctClassCFdbk> acctClassAFdbkC = acctDtlClassCMapper
                .selectList(cWrapper)
                .stream()
                .map(acctDtlClassC -> {
                    AcctClassCFdbk acctClassCFdbk = new AcctClassCFdbk();
                    try {
                        // 复制属性值
                        BeanUtils.copyProperties(acctDtlClassC, acctClassCFdbk);
                    } catch (Exception e) {
                        // 异常处理
                        e.printStackTrace();
                    }
                    // 将 Id 属性设置为空
                    acctClassCFdbk.setId(null);
                    return acctClassCFdbk;
                })
                .collect(Collectors.toList());

        this.saveBatch(acctClassAFdbkC);
    }
}
